<?xml version="1.0" encoding="utf-8"?>
<html
 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:atom="http://www.w3.org/2005/Atom"
 xmlns:ev="http://www.w3.org/2001/xml-events"
 xmlns:xf="http://www.w3.org/2002/xforms"
>
  <head>
    <script src="http://svn.x-port.net/svn/public/_Lib/xH.js" type="text/javascript">/**/</script>
    <title>GData Spreadsheet: Choose a worksheet</title>
    <style type="text/css">
      body
      {
        font-family : "Trebuchet MS", Verdana, Helvetica, Sans-Serif;
        font-size : 11px;
      }

      #main-body
      {
        margin : 0;
        background-color : #eee;
        border : 5px solid #333;
        padding : 15px;
        display : block;
      }
    </style>

    <xf:model>
      <!--
        The first instance manages the data for the request.
        
        For more details see:
        
          http://code.google.com/apis/accounts/AuthForInstalledApps.html
      -->
      <xf:instance>
        <instanceData xmlns="">
          <accountType>HOSTED_OR_GOOGLE</accountType>
          <Email />
          <Passwd />
          <service>wise</service>
          <source>gdataxformsclient-chooseworksheet-1.0</source>
        </instanceData>
      </xf:instance>

      <!--
        We use this instance to store the results of a request.
      -->
      <xf:instance id="inst-rs">
        <result xmlns="" />
      </xf:instance>
      
      <!--
        We use this instance to store the user's data.
      -->
      <xf:instance id="inst-user-data">
        <instanceData xmlns="">
          <authid />
          <spreadsheet-url />
          <worksheet-url />
        </instanceData>
      </xf:instance>

      <!--
        Here is the actual login request. At the beginning
        we toggle the 'busy' case, and at the end we toggle the
        'done' or 'error' case.
      -->
      <xf:submission
       method="urlencoded-post" resource="https://www.google.com/accounts/ClientLogin"
       separator="&amp;"
       replace="text" target="instance('inst-rs')"
      >
        <xf:toggle case="case-busy" ev:event="xforms-submit" />
        <xf:toggle case="case-error" ev:event="xforms-submit-error" />
        <xf:action ev:event="xforms-submit-done">
          <xf:setvalue
            ref="instance('inst-user-data')/authid"
            value="substring-after(instance('inst-rs'), 'Auth=')"
          />
          <xf:toggle case="case-done" />
        </xf:action>
      </xf:submission>

      <!--
        This submission retrieves a list of spreadsheets for a user.
      -->
      <xf:instance id="inst-spreadsheet-list-rs">
        <result xmlns="" />
      </xf:instance>

      <xf:submission id="sub-get-spreadsheet-list"
        method="get" resource="http://spreadsheets.google.com/feeds/spreadsheets/private/full"
        serialize="false"
        replace="instance" instance="inst-spreadsheet-list-rs"
      >
        <xf:header>
          <xf:name>Authorization</xf:name>
          <xf:value value="concat(
              'GoogleLogin auth=',
              instance('inst-user-data')/authid
            )"
          />
        </xf:header>
      </xf:submission>

      <!--
        This submission retrieves a spreadsheet, given its URL.
      -->
      <xf:instance id="inst-spreadsheet-rs">
        <result xmlns="" />
      </xf:instance>

      <xf:submission id="sub-get-spreadsheet"
        method="get"
        serialize="false"
        replace="instance" instance="inst-spreadsheet-rs"
      >
        <xf:resource value="instance('inst-user-data')/spreadsheet-url" />

        <xf:header>
          <xf:name>Authorization</xf:name>
          <xf:value value="concat(
              'GoogleLogin auth=',
              instance('inst-user-data')/authid
            )"
          />
        </xf:header>
      </xf:submission>
    </xf:model>
  </head>
  <body>
    <xf:group id="main-body">
      <!--
        Get the user's email and password.
      -->
      <xf:input ref="Email">
        <xf:label>Email:</xf:label>
      </xf:input>

      <xf:secret ref="Passwd">
        <xf:label>Password:</xf:label>
      </xf:secret>

      <!--
        This will do the login.
      -->
      <xf:submit>
        <xf:label>Get authentication token</xf:label>
      </xf:submit>

      <!--
        The various states that our form can be in are
        managed here:

          start: nothing has happened yet;
          busy:  a login is in progress;
          done:  a login is complete;
          error: an error occured.
      -->
      <xf:switch>
        <xf:case id="case-start">
          <div>Ready</div>
        </xf:case>

        <xf:case id="case-busy">
          <div>Busy</div>
        </xf:case>

        <xf:case id="case-done">
          <div>
            <h1>Result</h1>
            
            <!--
              Get a list of spreadsheets.
            -->
            <xf:submit submission="sub-get-spreadsheet-list">
              <xf:label>Get list of spreadsheets</xf:label>
            </xf:submit>

            <!--
              The first repeat shows the list of spreadsheets that a user has access to.
            -->
            <h2>Spreadsheets</h2>
            <xf:repeat id="rpt-spreadsheets" nodeset="instance('inst-spreadsheet-list-rs')/atom:entry">
              <xf:trigger appearance="minimal" style="display: block;">
                <xf:label value="atom:title" />
                <xf:action ev:event="DOMActivate">
                  <xf:setvalue
                    ref="instance('inst-user-data')/spreadsheet-url"
                    value="instance('inst-spreadsheet-list-rs')/atom:entry[index('rpt-spreadsheets')]/atom:link[@rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed' and @type='application/atom+xml']/@href"
                  />
                  <xf:send submission="sub-get-spreadsheet" />
                </xf:action>
              </xf:trigger>
            </xf:repeat>

            <!--
              The second repeat shows the list of worksheets from the selected spreadsheet.
            -->
            <h2>Worksheets</h2>
            <xf:repeat id="rpt-worksheets" nodeset="instance('inst-spreadsheet-rs')/atom:entry">
              <xf:trigger appearance="minimal" style="display: block;">
                <xf:label value="atom:title" />
                <xf:action ev:event="DOMActivate">
                  <xf:setvalue
                    ref="instance('inst-user-data')/worksheet-url"
                    value="instance('inst-spreadsheet-rs')/atom:entry[index('rpt-worksheets')]/atom:link[@rel='http://schemas.google.com/spreadsheets/2006#listfeed' and @type='application/atom+xml']/@href"
                  />
                </xf:action>
              </xf:trigger>
            </xf:repeat>

            <xf:output ref="instance('inst-user-data')/spreadsheet-url" style="display: block;">
              <xf:label>Spreadsheet URL:</xf:label>
            </xf:output>
            <xf:output ref="instance('inst-user-data')/worksheet-url" style="display: block;">
              <xf:label>Worksheet URL:</xf:label>
            </xf:output>
          </div>
        </xf:case>

        <xf:case id="case-error">
          <div>
            <h1>Error</h1>
            <xf:output value="substring-after(instance('inst-rs'), 'Error=')">
              <xf:label>Error:</xf:label>
            </xf:output>
          </div>
        </xf:case>
      </xf:switch>
    </xf:group>
  </body>
</html>
